import {Component, OnInit} from '@angular/core';
import * as data from './client.json'
import * as _ from 'lodash'

@Component({
  selector: 'app-welcome',
  templateUrl: './welcome.component.html',
  styleUrls: ['./welcome.component.scss']
})
export class WelcomeComponent implements OnInit {
  param = {
    language: 'java',
    bootVersion: data.bootVersion.default,
    packaging: 'jar',
    javaVersion: '1.8',
    groupId: data.groupId.default,
    artifactId: data.artifactId.default,
    name: data.name.default,
    description: data.description.default,
    packageName: data.packageName.default,
    dependencies: {},
  }

  data = data[`default`]

  ngOnInit(): void {
    const saveData = JSON.parse(localStorage.getItem('saveData'))
    if (saveData) {
      this.param = saveData
    }
  }

  submit() {
    this.save()
    this.download()
  }

  save() {
    localStorage.setItem('saveData', JSON.stringify(this.param))
  }

  download() {
    let dept = ''
    _.mapKeys(this.param.dependencies, (v, k) => {
      if (v == true) {
        dept += k + ','
      }
    })
    dept = dept.substr(0, dept.length - 1)
    window.location.href = `https://start.spring.io/starter.zip`
      + `?type=maven-project`
      + `&language=java`
      + `&bootVersion=${this.param.bootVersion}`
      + `&baseDir=${this.param.artifactId}`
      + `&groupId=${this.param.groupId}`
      + `&artifactId=${this.param.artifactId}`
      + `&name=${this.param.artifactId}`
      + `&description=` + encodeURIComponent(this.param.description)
      + `&packageName=${this.param.groupId}`
      + `&packaging=jar`
      + `&javaVersion=1.8`
      + `&dependencies=${dept}`
  }
}
